package com.shenmazong.serveruserapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.TbNo;
import com.shenmazong.TbUser;
import com.shenmazong.serveruserapi.mapper.ITbNoMapper;
import com.shenmazong.serveruserapi.mapper.ITbUserMapper;
import com.shenmazong.serveruserapi.service.TbNoService;
import com.shenmazong.serveruserapi.service.UserService;
import com.shenmazong.serveruserapi.util.ConstUtil;
import com.shenmazong.serveruserapi.util.RedisUtil;
import com.shenmazong.serveruserapi.util.SmsUtil;
import com.shenmazong.serveruserapi.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ITbUserMapper iTbUserMapper;

    @Autowired
    TbNoService tbNoService;

    @Override
    public Object sendSMS(String mobile) {
        //--1 生成6位验证码
        String code = String.valueOf((int)((Math.random()*9+1)*100000));

        //--2 验证码放到redis中
        String key = "code_" + mobile;
        redisUtil.set(key, code);
        redisUtil.expire(key, 60*5);

        //--3 发送验证码
        boolean isOK = SmsUtil.sendSmsBySanWu(mobile, code);

        //--4 生成返回值
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);

        return resultMap;
    }

    @Override
    public Object verifyCode(String mobile, String code) {

        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 获取验证码
        String key = "code_" + mobile;
        if(redisUtil.hasKey(key)) {
            resultMap.put("code", -1);
            resultMap.put("msg", "验证码错误");
            return resultMap;
        }

        //--2 比较验证码
        String redisCode = (String)redisUtil.get(code);
        if(!redisCode.equals(code)) {
            resultMap.put("code", -1);
            resultMap.put("msg", "验证码错误");
            return resultMap;
        }

        //--3 获取用户信息
        TbUser tbUser = iTbUserMapper.selectOne(
                new QueryWrapper<TbUser>().eq("mobile", mobile).last("limit 1")
        );
        if(tbUser != null) {
            String token = TokenUtil.getToken(tbUser.getId(), tbUser.getNickname());
            resultMap.put("code", 0);
            resultMap.put("token", token);
            resultMap.put("user", tbUser);
            return resultMap;
        }

        //--4 创建新用户
        TbUser user = new TbUser();
        user.setNickname("星星用户");
        user.setHeadurl(ConstUtil.USER_HEAD_URL);
        user.setMobile(mobile);

        TbNo userNo = (TbNo)redisUtil.lPop(ConstUtil.QUEUE_USER_NO);
        user.setUserno(userNo.getUserno());

        iTbUserMapper.insert(user);
        if(user.getId() > 0) {
            // 分配号码
            tbNoService.setUserNoUsing(userNo.getId(), userNo.getUserno());

            String token = TokenUtil.getToken(user.getId(), user.getNickname());
            resultMap.put("code", 0);
            resultMap.put("token", token);
            resultMap.put("user", user);
            return resultMap;
        }

        //--5 创建新用户失败了
        resultMap.put("code", -1);
        resultMap.put("msg", "用户创建失败了");
        return resultMap;
    }
}
